var fck_pstyle="text-indent:33px;line-height:30px;text-align: left;font-size: 16px;"; 
function formatText(editor) {
        var myeditor = editor;
        if (myeditor.mode == "wysiwyg") {
        	//所见即所得模式下
            var tempimg = new Array();
            var tempflash=new Array();
            var temptable = new Array();
            var tempobject = new Array();

            var isPart = false; //暂时无法实现局部格式化
            if (!isPart) {
                var tmpDiv = document.createElement("DIV");
                var editorhtml = myeditor.getData();
                editorhtml = editorhtml.replace(/<div style="page-break-after: always;?">\s*<span style="display: none;?">&nbsp;<\/span>\s*<\/div>/gi, '<p>[page]</p>');  //将div span标签替换为p 标签
                tmpDiv.innerHTML = editorhtml.replace(/&nbsp;/gi, '').replace(/<div/gi, '<p').replace(/<\/div/gi, '</p');     //移除空格标签，div替换为p标签。
                if (window.navigator.userAgent.toLowerCase().indexOf("msie") > 0) {
                    tmpDiv.innerHTML = tmpDiv.innerHTML.replace(/<\/p>/gi, '<br /><\/p>');      //每个段落相隔一行
                }
                var tables = tmpDiv.getElementsByTagName("TABLE");
                if (tables != null && tables.length > 0) {
                    for (var j = 0; j < tables.length; j++) {
                        //temptable[temptable.length] = tables[j].outerHTML;
                    	temptable[temptable.length] = $(tables[j].outerHTML);
                    }
                    var formattableCount = 0;
                    for (var j = 0; j < tables.length;) {
                        tables[j].outerHTML = "<BR/>#FormatTableID_" + formattableCount + "#<BR/>";
                        formattableCount++;
                    }
                }

                var objects = tmpDiv.getElementsByTagName("OBJECT");
                if (objects != null && objects.length > 0) {
                    for (var j = 0; j < objects.length; j++) {
                        tempobject[tempobject.length] = objects[j].outerHTML;
                    }
                    var formatobjectCount = 0;
                    for (var j = 0; j < objects.length;) {
                        objects[j].outerHTML = "<BR/>#FormatObjectID_" + formatobjectCount + "#<BR/>";
                        formatobjectCount++;
                    }
                }

                var imgs = tmpDiv.getElementsByTagName("IMG");
                if (imgs != null && imgs.length > 0) {
                    for (var j = 0; j < imgs.length; j++) {
                        //var t = document.createElement("IMG");
                        //var t = jQuery("<img src=\"\" style=\"\" border=\"0\"/>");
                        var outhtm=imgs[j].outerHTML;
                        var simg = $(outhtm);
//                        alert(outhtm);
                        /*var w = simg.css("width");
                        var h = simg.css("height");
                        var s = simg.attr("src");
                        if(w!='0px'){
                        	t.attr("width",w);
                        }
                        if(h!='0px'){
                        	t.attr("height",h);
                        }                 
                        if(s!=null){
                        	t.attr("src",s);
                        }else{
                        	t.attr("src","");
                        }*/
                        tempimg[tempimg.length] = simg;
                    }
                    var formatImgCount = 0;
                    for (var j = 0; j < imgs.length;) {
                        imgs[j].outerHTML = "<BR/>#FormatImgID_" + formatImgCount + "#<BR/>";
                        formatImgCount++;
                    }
                }
                var flashs = tmpDiv.getElementsByTagName("EMBED");
                if (flashs != null && flashs.length > 0) {
                    for (var j = 0; j < flashs.length; j++) {
                        //var t = document.createElement("IMG");
                        //var t = jQuery("<embed src=\"\" style=\"\" border=\"0\"/>");
                        var outhtm=flashs[j].outerHTML.toLowerCase();
                        var simg = $(outhtm);
                        /*var w = simg.css("width");
                        var h = simg.css("height");
                        var s = simg.attr("src");
                        var flashvars=simg.attr("flashvars");
                        if(w!='0px'){
                        	t.attr("width",w);
                        }
                        if(h!='0px'){
                        	t.attr("height",h);
                        }                 
                        if(s!=null){
                        	t.attr("src",s);
                        }else{
                        	t.attr("src","");
                        }
                        if(flashvars!=null){
                        	t.attr();
                        }*/
                        tempflash[tempflash.length] = simg;
                    }
                    var formatFlashCount = 0;
                    for (var j = 0; j < flashs.length;) {
                    	flashs[j].outerText = "<BR/>#FormatFlashID_" + formatFlashCount + "#<BR/>";
                        formatFlashCount++;
                    }
                }
                var strongarray = new Array();
                var strongcount = 0;
                for (var i = 0; i < tmpDiv.getElementsByTagName('b').length; i++) {
                    strongarray[strongcount] = tmpDiv.getElementsByTagName('b')[i].innerText.trim();
                    tmpDiv.getElementsByTagName('b')[i].innerHTML = "<BR/>#FormatStrongID_" + strongcount + "#<BR/>";
                    strongcount++;
                }

                for (var i = 0; i < tmpDiv.getElementsByTagName('strong').length; i++) {
                    strongarray[strongcount] = tmpDiv.getElementsByTagName('strong')[i].innerText.trim();
                    tmpDiv.getElementsByTagName('strong')[i].innerHTML = "<BR/>#FormatStrongID_" + strongcount + "#<BR/>";
                    strongcount++;
                }
                var html = processFormatText(tmpDiv.innerText);
//                alert(html);
                html = html.replace(/<p>\[page\]<\/p>/gi, '<div style="page-break-after: always;"><span style="display: none;">&nbsp;</span></div>');   //p标签替换回原来的div和span标签。
                if (temptable != null && temptable.length > 0) {
                    for (var j = 0; j < temptable.length; j++) {
                    	var div = $("<div></div>");
                    	temptable[j].css("margin","");
                    	temptable[j].attr("align","center");
                    	div.append(temptable[j]);
                        var tablehtml = div.html();
                        //var tablehtml = "<div align=\"center\">"+temptable[j]+"</div>";
                        html = html.replace("#FormatTableID_" + j + "#", tablehtml);
                    }
                }

                if (tempobject != null && tempobject.length > 0) {
                    for (var j = 0; j < tempobject.length; j++) {
                        var objecthtml = "<p align=\"center\">" + tempobject[j] + "</p>";
                        html = html.replace("#FormatObjectID_" + j + "#", objecthtml);
                    }
                }

                if (tempimg != null && tempimg.length > 0) {
                    for (var j = 0; j < tempimg.length; j++) {
                    	var div = $("<div></div>");
                    	div.append(tempimg[j]);
                        var imghtml="<p align=\"center\">"+div.html()+"</p>";
                        html = html.replace("#FormatImgID_" + j + "#", imghtml);
                    }
                }
                
                if (tempflash != null && tempflash.length > 0) {
                    for (var j = 0; j < tempflash.length; j++) {
                    	var div = $("<div></div>");
                    	div.append(tempflash[j]);
                        var imghtml="<p align=\"center\">"+div.html()+"</p>";
                        html = html.replace("#FormatFlashID_" + j + "#", imghtml);
                    }
                }

                for (var i = 0; i < strongcount; i++) {
                    html = html.replace("#FormatStrongID_" + i + "#", "<p><strong>" + strongarray[i] + "</strong></p>");
                }

                while (html.indexOf("</p></p>") != -1) html = html.replace("</p></p>", "</p>");
                while (html.indexOf('<p><p align="center">') != -1) html = html.replace('<p><p align="center">', '<p align="center">');
                editor.setData(html);

            } else {

            }
        } else {
            alert('必须在设计模式下操作！');
        }
    }

    function processFormatText(textContext) {
        var text = dbc2Sbc(toTxt(textContext));
        var prefix = "　　";
        var tmps = text.split("\n");
        var html = "";
        for (var i = 0; i < tmps.length; i++) {
            var tmp = tmps[i].trim();
            if (tmp.length > 0) {
                var reg = /#Format[A-Za-z]+_\d+#/gi;
                var f = reg.exec(tmp);
                if (f != null) {
                    tmp = tmp.replace(/#Format[A-Za-z]+_\d+#/gi, '');
                    html += f;
                    if (tmp != ""){
                    	//html += "<p align=\"center\">" + tmp + "</p>\n";
                    	html += "<p style=\""+fck_pstyle+"\">" + tmp + "</p>\n";                    	
                    }
                } else {
                	html += "<p style=\""+fck_pstyle+"\">" + tmp + "</p>\n";
                    //html += "<p style='text-indent:2em;'>" + tmp + "</p>\n";
                }
            }
        }
        /*
        var reg = /#Format[A-Za-z]+_\d+#/gi;
        var f ;
        while ((f= reg.exec(text))!= null) {
            html += f;
            if (text != ""){
            	//html += "<p align=\"center\">" + tmp + "</p>\n";
            	html += "<p style=\""+fck_pstyle+"\">" + tmp + "</p>\n";                    	
            }
        }
        text = text.replace(/#Format[A-Za-z]+_\d+#/gi, '');
        */
        return html;
    }

    function dbc2Sbc(str) {
        var result = '';
        for (var i = 0; i < str.length; i++) {
            var code = str.charCodeAt(i);
            // “65281”是“！”，“65373”是“｝”，“65292”是“，”。不转换"，"

            if (code >= 65281 && code < 65373 && code != 65292 && code != 65306 && code !=65307) {
                //  “65248”是转换码距
                result += String.fromCharCode(str.charCodeAt(i) - 65248);
            } else {
                result += str.charAt(i);
            }
        }
        return result;
    }

    String.prototype.trim = function () {
        return this.replace(/(^[\s　]*)|([\s　]*$)/g, "");
    };

    String.prototype.leftTrim = function () {
        return this.replace(/(^\s*)/g, "");
    };

    String.prototype.rightTrim = function () {
        return this.replace(/(\s*$)/g, "");
    };
    function toTxt(str) {
    	var RexStr = /\<|\>|\"|\'|\&/g
    	str = str.replace(RexStr, function(MatchStr) {
    		switch (MatchStr) {
    		case "<":
    			return "&lt;";
    			break;
    		case ">":
    			return "&gt;";
    			break;
    		case "\"":
    			return "&quot;";
    			break;
    		case "'":
    			return "&#39;";
    			break;
    		case "&":
    			return "&amp;";
    			break;
    		default:
    			break;
    		}
    	});
    	return str;
    }
    function preFormat(str){
    	/*
    	 * 先将特殊字符替换成看不懂的字符
    	*/
    	
    	var RexStr = /\<|\>|\"|\'|\&/g
    	str = str.replace(RexStr, function(MatchStr) {
    		switch (MatchStr) {
    		case "&lt;":
    			return "&z_lt";
    			break;
    		case "&gt;":
    			return "&z_gt";
    			break;
    		case "&quot;":
    			return "&z_quot";
    			break;
    		case "&#39;":
    			return "&z_#39";
    			break;
    		case "&amp;":
    			return "&z_amp";
    			break;
    		default:
    			break;
    		}
    	});
    	return str;
    	
    }
    function afterFormat(str){
    	/*
    	 * 最后将特殊字符替换成代码字符
    	*/
    	var RexStr = /\<|\>|\"|\'|\&/g
    	str = str.replace(RexStr, function(MatchStr) {
    		switch (MatchStr) {
    		case "&z_lt":
    			return "&lt;";
    			break;
    		case "&z_gt":
    			return "&gt;";
    			break;
    		case "&z_quot":
    			return "&quot;";
    			break;
    		case "&z_#39":
    			return "&#39;";
    			break;
    		case "&z_amp":
    			return "&amp;";
    			break;
    		default:
    			break;
    		}
    	});
    	return str;
    	
    }
    
